<?php         
class Recipe_model extends MY_Model {
	var $table_name = 'recipe';
	var $primary_key = 'id';
//        function get_by_id($id)
//    {
//        $this->db->where("{$this->table_name}.{$this->primary_key}",$id);
//        $this->db->join('folder_picture', "{$this->table_name}.images = folder_picture.folder_id AND folder_picture.order=1", 'left');
//    	$this->db->join('picture', "picture.id = folder_picture.picture_id", 'left');
//    	$this->db->group_by("{$this->table_name}.id");
//        $query = $this->db->get($this->table_name);
//        return $query->row_array();
//    }
//	function get_list($start=null,$limit=null,$sort= '',$dir='DESC',$where = null)
//    {
//            $fields = 'recipe.*,picture.url';
//
//    	$this->db->select($fields);
//		$this->db->from($this->table_name);
//		if(!empty($where)) $this->db->where($where);
//    	$this->db->join('folder_picture', "{$this->table_name}.images = folder_picture.folder_id AND folder_picture.order=1", 'left');
//    	$this->db->join('picture', "picture.id = folder_picture.picture_id", 'left');
//
//    	if(empty($sort)) $this->db->order_by($this->primary_key, $dir);
//    	else $this->db->order_by($sort, $dir);
//		$this->db->order_by('folder_picture.order', 'ASC');
//		$this->db->group_by("{$this->table_name}.id");
//    	if(!empty($limit)) $this->db->limit($limit,$start);
//
//
//	    $query = $this->db->get();
//
//		return $query->result_array();
//
//    }
    function parent_getlist($start=null,$limit=null,$sort= '',$dir='DESC',$where = null,$fields='*'){
       return parent::get_list($start, $limit, $sort, $dir, $where, $fields);
    }
    function updateOrder($oldorder,$order){
        if (!is_numeric($oldorder)||!is_numeric($order))throw new Exception('Error in Recipe Model');
        if ($oldorder < $order){
            $sql = "update {$this->table_name} set `order` = `order` - 1 where `order` <= $order and `order` > $oldorder";
        }else{
            $sql = "update {$this->table_name} set `order` = `order` + 1 where `order` < $oldorder and `order` >= $order";
        }
        return $this->db->query($sql);
    }
     function  delete($id) {
        $data = $this->get_by_id($id);
        $sql = "update {$this->table_name} set `order` = `order` - 1 where `order` > {$data['order']}";
        $this->db->query($sql);
        return parent::delete($id);
    }
}  
     ?>